package com.rgs.se.algorithm.common;

/**
 * @author Ren Gaoshuai
 * @ClassName NoResivice
 * @Description 非递归实现斐波那契数列
 * @date 2021-10-27 9:48
 **/
public class NoRecursive {

    /**
     * 参数的替换
     * 第二个值替换为第一个值
     * 二者的和替换第二个值
     * 一次向后
     * 注意特殊情况 n=0 n=1
     */
    public int noRecursiveFib(int n){
        if (n < 0){
            System.out.println(n+",<0参数非法!");
        }
        if (n == 1 || n == 0){
            return n;
        }
        int fib1 = 0;
        int fib2 =  1;
        int sum = 0 ;
        for (int i = 2; i <= n; i++) {
            sum = fib1 + fib2;
            fib1 = fib2;
            fib2 = sum;
            sum = 0;
        }
        return fib2;
    }

    public static void main(String[] args) {
        NoRecursive recursive = new NoRecursive();
        for (int j = 0; j < 10; j++) {

            System.out.print(recursive.noRecursiveFib(j)+",");
        }
    }
}
